﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DAL;
using Entity;

namespace BLL
{
    public class RecordBO
    {      
        /// <summary>
        /// 查找预约状态的枚举项;
        /// </summary>
        public static AppointmentRecord.RecordStatus FindRecordStatus(string ToFind)
        {
            try
            {
                AppointmentRecord.RecordStatus status = (AppointmentRecord.RecordStatus)Enum.Parse(typeof(AppointmentRecord.RecordStatus), ToFind);
                return status;
            }
            catch (Exception ex)
            {
                return AppointmentRecord.RecordStatus.UnDefined;
            }
        }

        /// <summary>
        /// 获得某项服务在某天每一时段的已预约人数;
        /// </summary>
        /// <param name="serviceId"></param>
        /// <param name="day"></param>
        /// <param name="timeBucket"></param>
        /// <returns></returns>
        public static int GetReservedNum(int serviceId, string day, string timeBucket)
        {
            return RecordDAO.GetReservedNum(serviceId, day, timeBucket);
        }

        /// <summary>
        /// 写入预约记录;
        /// </summary>
        public static int InsertRecord(AppointmentRecord record)
        {
            return RecordDAO.InsertRecord(record);
        }

        /// <summary>
        /// 为学生或负责人获取历史预约记录;
        /// </summary>
        /// <param name="user"></param>
        /// <param name="loginType">Student、Director</param>
        /// <returns></returns>
        public static DataTable GetRecordsTable(int user, string loginType)
        {
            return RecordDAO.GetRecordsTable(user, loginType);
        }

        /// <summary>
        /// 获取只填充了普通字段的预约记录对象;
        /// </summary>
        /// <param name="appointmentId"></param>
        /// <returns></returns>
        public static AppointmentRecord GetRecordCommon(int appointmentId)
        {
            AppointmentRecord record = new AppointmentRecord();

            DataTable dt = RecordDAO.GetRecordCommonTable(appointmentId);
            if (dt != null && dt.Rows.Count > 0)
            {
                record.AppointmentId = appointmentId;
                record.ServiceId = (int)dt.Rows[0]["ServiceId"];
                record.ApplicantId = (int)dt.Rows[0]["ApplicantId"];
                record.Day = dt.Rows[0]["Day"] as string;
                record.TimeBucket = dt.Rows[0]["TimeBucket"] as string;
                record.Status = dt.Rows[0]["Status"] as string;
                record.ApprovalStatus = dt.Rows[0]["ApprovalStatus"] as string;
                record.CreatTime = (DateTime)dt.Rows[0]["CreatTime"];
                if (dt.Rows[0]["ApproveTime"] != DBNull.Value)
                {
                    record.ApproveTime = (DateTime)dt.Rows[0]["ApproveTime"];
                }
                record.Comment = dt.Rows[0]["Comment"] as string ?? string.Empty;
                record.ApprovalComment = dt.Rows[0]["ApprovalComment"] as string ?? string.Empty;
            }

            return record;
        }

        /// <summary>
        /// 获取预约记录详情;
        /// </summary>
        /// <param name="appointmentId"></param>
        /// <returns>AppointmentRecord对象</returns>
        public static AppointmentRecord GetRecordDetail(int appointmentId)
        {
            AppointmentRecord record = GetRecordCommon(appointmentId);

            record.Service = ServiceBO.GetServiceDetail(record.ServiceId); // 服务详情;
            record.Applicant = UserBO.GetUserById(record.ApplicantId); // 申请者详情;

            return record;
        }

        /// <summary>
        /// 负责人同意或拒绝预约记录;
        /// </summary>
        /// <param name="recordId"></param>
        /// <param name="status">"Approve"或"Reject"</param>
        /// <param name="approvalComment">批注内容</param>
        public static int UpdateRecordStatusByDirector(int recordId, string status, string approvalComment)
        {
            return RecordDAO.UpdateRecordStatusByDirector(recordId, status, approvalComment);
        }

        /// <summary>
        /// 学生对预约进行评价;
        /// </summary>
        public static int StudentMakeComment(int recordId, string comment)
        {
            return RecordDAO.StudentMakeComment(recordId, comment);
        }
    }
}
